﻿using Corina.Datamodel.Entities;
using Corina.Infrastructure;
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Corina.Commands;
using Corina.Common;
using Corina.Enumerations;

namespace Corina.Lantern.Hub.Viewmodels
{
    public class RegisterViewmodel : ViewmodelBase
    {
        public RegisterViewmodel() {
            CommonClient.GetAllUserkindCompleted += GetAllUserkindCompleted;
            CommonClient.GetAllIndustryCompleted += GetAllIndustryCompleted;
            UserClient.RegisterCompleted += RegisterCompleted;
            if (!IsDesignMode)
            {
                LoadData();
            }
        }

        #region Methods
        private void RegisterCompleted(object sender, DataService.RegisterCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (e.Result.Result == ResultValue.Succeed)
                {
                    DataContext.ViewUrl = "/Login";
                }
                else
                {
                    MessageBox.Show(GetOperateResult(e.Result.Result));
                }
            }
            else
            { }
            IsBusy = false;

        }

        private void GetAllIndustryCompleted(object sender, DataService.GetAllIndustryCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                IndustryCollection = new ObservableCollection<IndustryInfo>(e.Result.Entities.OfType<IndustryInfo>());
            }
            else
            { }
        }

        private void GetAllUserkindCompleted(object sender, DataService.GetAllUserkindCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                UserKindCollection = new ObservableCollection<UserKindInfo>(e.Result.Entities.OfType<UserKindInfo>());
            }
            else
            { 
                
            }
        }

        protected override void LoadData(params object[] paras)
        {
            CommonClient.GetAllIndustryAsync();
            CommonClient.GetAllUserkindAsync();
        } 
        #endregion

        #region Collection
        private ObservableCollection<UserKindInfo> _userKindCollection;
        public ObservableCollection<UserKindInfo> UserKindCollection
        {
            get { return _userKindCollection; }
            set
            {
                _userKindCollection = value;
                RaisePropertyChanged(() => UserKindCollection);
            }
        }

        private ObservableCollection<IndustryInfo> _industryCollection;
        public ObservableCollection<IndustryInfo> IndustryCollection
        {
            get { return _industryCollection; }
            set
            {
                _industryCollection = value;
                RaisePropertyChanged(() => IndustryCollection);
            }
        }
        #endregion

        #region Properties
        private UserInfo _updateEntity = new UserInfo();
        public UserInfo UpdateEntity
        {
            get { return _updateEntity; }
            set
            {
                _updateEntity = value;
                RaisePropertyChanged(() => UpdateEntity);
            }
        }

        private string _confirmPassword = string.Empty;
        public string ConfirmPassword
        {
            get { return _confirmPassword; }
            set
            {
                _confirmPassword = value;
                RaisePropertyChanged(() => ConfirmPassword);
                RaisePropertyChanged(() => SubmitCommand);
            }
        }

        private bool _agreePolicy = false;
        public bool AgreePolicy
        {
            get { return _agreePolicy; }
            set
            {
                _agreePolicy = value;
                RaisePropertyChanged(() => AgreePolicy);
                RaisePropertyChanged(() => SubmitCommand);
            }
        }

        private IndustryInfo _selectedIndustry;
        public IndustryInfo SelectedIndustry
        {
            get { return _selectedIndustry; }
            set
            {
                _selectedIndustry = value;
                RaisePropertyChanged(() => SelectedIndustry);
                UpdateEntity.IndustryId = SelectedIndustry == null ? 0 : SelectedIndustry.Id;
                RaisePropertyChanged(() => SubmitCommand);
            }
        }

        private UserKindInfo _selectedUserKind;
        public UserKindInfo SelectedUserKind
        {
            get { return _selectedUserKind; }
            set
            {
                _selectedUserKind = value;
                RaisePropertyChanged(() => SelectedUserKind);
                UpdateEntity.UserKindId = SelectedUserKind == null ? 0 : SelectedUserKind.Id;
                RaisePropertyChanged(() => SubmitCommand);
            }
        }

        public bool PasswordNotMatched
        {
            get
            {
                return ConfirmPassword != UpdateEntity.Password;
            }
        }
        #endregion

        #region Events
        public void TextChanged(object sender,TextChangedEventArgs e)
        {
            RaisePropertyChanged(() => SubmitCommand);
        }

        public void PasswordChanged(object sender, RoutedEventArgs e)
        {
            RaisePropertyChanged(() => SubmitCommand);
            RaisePropertyChanged(() => PasswordNotMatched);
        }

        public void LostFocus(object sender,RoutedEventArgs e)
        {
            RaisePropertyChanged(() => PasswordNotMatched);
        }
        #endregion

        #region Commands
        public ICommand SubmitCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return UpdateEntity.Account.Trim().Length > 4 && UpdateEntity.Password.Length > 6 && UpdateEntity.Password == ConfirmPassword && UpdateEntity.UserKindId > 0 && AgreePolicy; },
                    ExecuteCallback = delegate
                    {
                        IsBusy = true;
                        string jsonString = JsonHelper.Stringify(UpdateEntity);
                        UserClient.RegisterAsync(jsonString);
                    }
                };
            }
        }

        public ICommand GotoLoginCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate {
                        DataContext.ViewUrl = "/Login";
                    }
                };
            }
        }
        #endregion

    }
}
